package com.hitrust.gs.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import system.LoginContext;

import com.hitrust.gs.bean.GSOrderAnaDetailAndCategoryBean;
import com.hitrust.gs.bean.GSOrderAnaDetailBean;
import com.hitrust.gs.bean.GSOrderBean;
import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.business.GSOrderProcessor;
import com.hitrust.gs.business.GSUniteGwProcessor;
import com.hitrust.gs.dao.GSOrderDAO;
import com.hitrust.gs.form.GSOrderDetailQueryForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.DefineDic;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.util.page.ResultProcess;

public class GSOrderDetailOfBusQryExportAction extends GSBaseAction {

	public String execute(GSBaseForm aForm) throws Exception {
		GSOrderDetailQueryForm tForm = (GSOrderDetailQueryForm) aForm;
		
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		//ServletOutputStream out = null;
		ServletOutputStream out = null;
		HSSFWorkbook wb = null;
		
		try {
			tForm.getRequest().setAttribute("orderid", tForm.getOrderid());
			tForm.getRequest().setAttribute("providerName", tForm.getProviderName());
			tForm.getRequest().setAttribute("concentricityId", tForm.getConcentricityId());
			tForm.getRequest().setAttribute("orderCreateDateStr", tForm.getOrderCreateDateStr());
			tForm.getRequest().setAttribute("orderCreateDateEnd", tForm.getOrderCreateDateEnd());
			tForm.getRequest().setAttribute("manufInfo", tForm.getManufInfo());
			tForm.getRequest().setAttribute("orderDetailStatus", tForm.getOrderDetailStatus());
			tForm.getRequest().setAttribute("deliverGoodsId", tForm.getDeliverGoodsId());
			tForm.getRequest().setAttribute("orderType", tForm.getOrderType());
			tForm.getRequest().setAttribute("drawingpiececode", tForm.getDrawingpiececode());

			String[] tDateStr = new String[2];
			tDateStr[0] = tForm.getOrderCreateDateStr();
			tDateStr[1] = tForm.getOrderCreateDateEnd();
			tDateStr = DateUtil.formateDate(tDateStr);
			tForm.setOrderCreateDateStr(tDateStr[0]);
			tForm.setOrderCreateDateEnd(tDateStr[1]);

			LoginContext context = (LoginContext) aForm.getSession().getAttribute(GSBaseConstants.LOGIN);

			GSUniteGwProcessor unitePro = new GSUniteGwProcessor();
			ArrayList wareList = unitePro.getWareHouseLevelName(context.getLoginWarehouseCode(),context.getLoginWarehouseCode());
			aForm.getRequest().setAttribute("orderdept", wareList);
			GSOrderProcessor tProc=new GSOrderProcessor();
			//取得分页参数
			//String[] tStr = getPageParams(tForm);
			//执行分页查询
			ArrayList tList = tProc.getOrderDetailList(tForm,1,99999999);
			ResultProcess tRsProc = new ResultProcess();
			tRsProc.setResult(tList);
			
			String xls = "Order.xls";
            aForm.getResponse().setContentType("application/ms-excel");

    		aForm.getResponse().setHeader("Content-disposition", "attachment; filename=" + xls);
    		out = aForm.getResponse().getOutputStream();
    		wb = this.getExportExcel(tRsProc.getAllRecoreds(), aForm.getRequest());
    		wb.write(out);

//			tForm.getRequest().setAttribute("order", tRsProc);
//
//			//存储分页参数
//			tForm.getRequest().setAttribute("pageSize",tStr[1]);
//			tForm.getRequest().setAttribute("totalRecords",tStr[2]);
//
//			//取得订单明细状态
//			aForm.getRequest().setAttribute("orderdetailstatuslist", getOrderDetailStatusList());
//
//			//取得订单明细状态
//			aForm.getRequest().setAttribute("ordertypelist", getOrderTypeList());
//
//			// 取得订单总金额
//			double tTotalAmount = tProc.getOrderDetailTotalAmount(tForm);
//			DecimalFormat df = new DecimalFormat("##0.00##");
//			String totalAmount = df.format(tTotalAmount);
//			aForm.getRequest().setAttribute("totalAmount", totalAmount);
//
//			//取得产品分类
//			GSManufactureProcessor tManufProc = new GSManufactureProcessor();
//			tManufProc.getManufactureOfLevel(aForm);

		} catch (Exception e) {
			tForm.getRequest().setAttribute("action", "A150501.do");
			throw e;
		}finally{
        	try {
				if (bos != null){
					bos.flush();
					bos.close();
				}
				if (bis != null){
					bis.close();
				}
				if (out != null){
					out.flush();
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
        return null;
	}
  
	private HSSFWorkbook getExportExcel(Collection list, HttpServletRequest request) {


		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String strDate = sdf.format(new Date());
	
			HSSFWorkbook wb = new HSSFWorkbook();
			HSSFCellStyle titleStyle = wb.createCellStyle();
			titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平居中
			titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
			HSSFSheet sheet = wb.createSheet();
			
			int fistColumn = 0;					
			
			HSSFRow titleRow = sheet.createRow((short) fistColumn);
			
			HSSFCell titlecell = titleRow.createCell((short) 0);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("订单明细编号");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 1);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("集中编码");
			titlecell.setCellStyle(createTopTitleStyle(wb));
					
			titlecell = titleRow.createCell((short) 2);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("产品名称");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 3);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("产品类型");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 4);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("规格");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 5);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("型号");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 6);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("品牌");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 7);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("图件号");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 8);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("供应商");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 9);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("订货数量");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 10);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("订单明细状态");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 11);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("创建时间");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 12);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("订货单位");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 13);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("单价（元）");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 14);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("总价（元）");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 15);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("送货日期");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 16);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("送货时间");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			titlecell = titleRow.createCell((short) 17);
			titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
			titlecell.setCellValue("结算模式");
			titlecell.setCellStyle(createTopTitleStyle(wb));
			
			Iterator iter = list.iterator();
			int row = 1; 
			while(iter.hasNext()) {
				GSOrderAnaDetailAndCategoryBean tGSOrderVO = (GSOrderAnaDetailAndCategoryBean)iter.next();
				HSSFRow deptrow = sheet.createRow((short) row);
				
				HSSFCell cell = deptrow.createCell((short) 0 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(tGSOrderVO.getGs_order_detail_id())){
					cell.setCellValue(tGSOrderVO.getGs_order_detail_id());
				}
				
				cell = deptrow.createCell((short) 1 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(tGSOrderVO.getGs_concentricity_id())){
					cell.setCellValue(tGSOrderVO.getGs_concentricity_id());
				}		
				
				cell = deptrow.createCell((short) 2 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(tGSOrderVO.getName_product())){
					cell.setCellValue(tGSOrderVO.getName_product());
				}
				
				cell = deptrow.createCell((short) 3 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(GSBaseManager.getProductTypeByCode(tGSOrderVO.getIs_normal()))){
					cell.setCellValue(GSBaseManager.getProductTypeByCode(tGSOrderVO.getIs_normal()));
				}
				
				cell = deptrow.createCell((short) 4 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(tGSOrderVO.getSpec())){
					cell.setCellValue(tGSOrderVO.getSpec());
				}
				
				cell = deptrow.createCell((short) 5 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(tGSOrderVO.getModel())){
					cell.setCellValue(tGSOrderVO.getModel());
				}
				
				cell = deptrow.createCell((short) 6 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(tGSOrderVO.getProduct_brand())){
					cell.setCellValue(tGSOrderVO.getProduct_brand());
				}
				
				cell = deptrow.createCell((short) 7 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(tGSOrderVO.getDrawing_piece_code())){
					cell.setCellValue(tGSOrderVO.getDrawing_piece_code());
				}
				
				cell = deptrow.createCell((short) 8 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);		
				if(StringUtils.isNotEmpty(tGSOrderVO.getName())){
					cell.setCellValue(tGSOrderVO.getName());
				}
				
				cell = deptrow.createCell((short) 9 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);			
				cell.setCellValue(com.hitrust.gs.util.lang.StringUtils.format(tGSOrderVO.getGs_order_count(),2)+ tGSOrderVO.getProduct_unit());

				
				cell = deptrow.createCell((short) 10 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(GSBaseManager.getOrderDetailStatusName(tGSOrderVO.getGs_order_detail_status()))){
					cell.setCellValue(GSBaseManager.getOrderDetailStatusName(tGSOrderVO.getGs_order_detail_status()));
				}
				
				cell = deptrow.createCell((short) 11 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(tGSOrderVO.getGs_order_create_date())){
					cell.setCellValue(tGSOrderVO.getGs_order_create_date());
				}
				
				cell = deptrow.createCell((short) 12 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(tGSOrderVO.getName_warehouse())){
					cell.setCellValue(tGSOrderVO.getName_warehouse());
				}
				
				cell = deptrow.createCell((short) 13 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(com.hitrust.gs.util.lang.StringUtils.format(tGSOrderVO.getGs_goods_unitprice(),4))){
					cell.setCellValue(com.hitrust.gs.util.lang.StringUtils.format(tGSOrderVO.getGs_goods_unitprice(),4));
				}
				
				cell = deptrow.createCell((short) 14 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(com.hitrust.gs.util.lang.StringUtils.format(tGSOrderVO.getGs_order_count()*tGSOrderVO.getGs_goods_unitprice(),4))){
					cell.setCellValue(com.hitrust.gs.util.lang.StringUtils.format(tGSOrderVO.getGs_order_count()*tGSOrderVO.getGs_goods_unitprice(),4));
				}
				
				cell = deptrow.createCell((short) 15 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(tGSOrderVO.getGs_deliver_goods_date())){
					cell.setCellValue(tGSOrderVO.getGs_deliver_goods_date());
				}
				
				cell = deptrow.createCell((short) 16 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(tGSOrderVO.getDeliverGoodsTimeInfo(tGSOrderVO.getGs_deliver_goods_time()))){
					cell.setCellValue(tGSOrderVO.getDeliverGoodsTimeInfo(tGSOrderVO.getGs_deliver_goods_time()));
				}
				
				cell = deptrow.createCell((short) 17 );
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(StringUtils.isNotEmpty(DefineDic.getBalanceMode(tGSOrderVO.getGs_balance_mode()))){
					cell.setCellValue(DefineDic.getBalanceMode(tGSOrderVO.getGs_balance_mode()));
				}
				row ++;
			}		
		return wb;	
	}

	public static HSSFCellStyle createTopTitleStyle(HSSFWorkbook wb){
		HSSFCellStyle cellstyle = wb.createCellStyle();
		cellstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直   
		cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平   
		return cellstyle;
	}
}
